package com.shengqiang.cloud.ups.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.shengqiang.cloud.ups.common.model.JsonResult;
import com.shengqiang.cloud.ups.common.model.PageResult;
import com.shengqiang.cloud.ups.dao.modal.UpmsRole;
import com.shengqiang.cloud.ups.dao.repository.master.UpmsRoleMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import springfox.documentation.spring.web.json.Json;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/1/1.
 */
@Service
public class UpmsRoleWebService extends ServiceImpl<UpmsRoleMapper,UpmsRole>{

    private static final Logger logger= LoggerFactory.getLogger(UpmsRoleWebService.class);

    public PageResult<UpmsRole> selectRoles(Map<String,Object> params){
        PageResult<UpmsRole> result=new PageResult<>(1,"fail",0,null);
        try{
            int current=Integer.parseInt(params.get("page").toString());
            int limit=Integer.parseInt(params.get("limit").toString());
            Page<UpmsRole> page=new Page<>(current,limit);
            page.setRecords(baseMapper.selectRoles(page,params));
            if(page!=null){
                if(page.getRecords()!=null){
                    result.setCode(0);
                    result.setMsg("success");
                    result.setCount(page.getTotal());
                    result.setData(page.getRecords());
                }
            }
        }catch (Exception ex){
            logger.error("查询角色列表出现异常:{}",ex.getMessage(),ex);
        }
        return result;
    }

    public JsonResult<List<UpmsRole>> selectAllRoles(Map<String,Object> params){
        try{
            List<UpmsRole> roles=baseMapper.selectRoles(params);
            return new JsonResult<List<UpmsRole>>(0,"success",roles);
        }catch (Exception ex){
            return new JsonResult<>(1,"查询角色列表出现错误");
        }
    }

    public JsonResult addRole(UpmsRole role){
        JsonResult result=null;
        try{
            int count=baseMapper.insert(role);
            if(count>0){
                result=new JsonResult(0,"添加角色【"+role.getName()+"】成功");
            }else{
                result=new JsonResult(1,"添加角色【"+role.getName()+"】失败");
            }
        }catch (Exception ex){
            logger.error("添加角色【"+role.getName()+"】失败:{}",ex.getMessage(),ex);
            result=new JsonResult(1,"添加角色【"+role.getName()+"】失败");
        }
        return result;
    }

    /**
     * 删除角色信息
     * @param ids
     * @return
     */
    public JsonResult deleteRoleByIds(List<Integer> ids){
        JsonResult result=null;
        try{
            int count=baseMapper.deleteBatchIds(ids);
            if(count>0){
                result=new JsonResult(0,"删除成功");
            }else{
                result=new JsonResult(1,"删除角色信息失败");
            }
        }catch (Exception ex){
            logger.error("同步角色Id删除角色信息出现异常:{}",ex.getMessage(),ex);
            result=new JsonResult(1,"删除角色信息失败");
        }
        return result;
    }

    public JsonResult updateRoleById(UpmsRole role){
        JsonResult result=new JsonResult(1,"修改失败");
        try{
            int rowCount=baseMapper.updateById(role);
            if(rowCount>0){
                result.setCode(0);
                result.setMsg("修改成功");
            }
        }catch (Exception ex){
            logger.error("通过Id修改角色信息出现异常:{}",ex.getMessage());
        }
        return result;
    }
}
